
package com.haipumi.back.actions;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import com.haipumi.actions.model.PageModel;
import com.haipumi.beans.DataGrid;
import com.haipumi.beans.JsonModel;
import com.haipumi.beans.haipumi_task;
import com.haipumi.biz.IBackTaskBiz;
import com.haipumi.webSocket.CustomerServiceWebsocket;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


//任务状态		default 1未完成 2完成 3审核中 4冻结 5过期
public class BackTaskAction extends CommonAction implements ModelDriven<haipumi_task>{

	@Autowired
	IBackTaskBiz backTaskBiz;
	
	
	private static final long serialVersionUID = 3498134975698225891L;
	
	
	private DataGrid dg=new DataGrid();

	private String op;
	private String searchType;
	private haipumi_task task=new haipumi_task();

	@Override
	public String execute() throws Exception {
		if(!"".equals(op)&&op!=null){
			if("findTask".equals(op)){
				findTask( );
			}else if("addTask".equals(op)){
				addTask( );
			}else if("updateTask".equals(op)){
				updateTask( );
			}else if("checkTask".equals(op)){
				checkTask( );
			}else if("caozuotask".equals(op)){
				caozuotask( );
			}else if("finishtask".equals(op)){
				finishtask( );
			}
		}
		return Action.SUCCESS;
	}


	private void finishtask( ) {
		String tid=task.getTid();
		boolean flag=false;
		flag=backTaskBiz.finishTask(tid);
		if(flag){
			dg.setCode(1);
			dg.setMsg("修改成功，任务完成");
		}else{
			dg.setCode(0);
		}
	}

	private void caozuotask( ) {
		String tid=task.getTid();
		boolean flag=false;
		flag=backTaskBiz.caozuo(tid, searchType);
		if(flag){
			dg.setCode(1);
			dg.setMsg("成功执行...");
		}else {
			dg.setCode(0);
			dg.setMsg("操作失败，请联系管理员...");
		}
	}

	private void checkTask( ) {
		String tid=task.getTid();
		boolean flag=backTaskBiz.checkTask(tid);
		//TODO 审批处理后
		if(flag){
			dg.setCode(1);
			dg.setMsg("审批通过");
			CustomerServiceWebsocket.pushMessage("{\"fromId\":\"1\",\"message\":\"1\"}");
		}else{
			dg.setCode(0);
			dg.setMsg("审批通过失败");
		}
	}

	private void updateTask( ) {
		// TODO 更新任务，暂时不
	}

	private void addTask() {
		// TODO  后台添加官方发布
	}

	private void findTask( ) {
		List<haipumi_task> tasklist=backTaskBiz.findAllTask(start,end,sort,order);
		int r=0;
		r=backTaskBiz.countAllTask(start,end,sort,order);
		dg.setRows(tasklist);
		dg.setTotal(r);
	}

	@Override
	public haipumi_task getModel() {
		return task;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public void setTask(haipumi_task task) {
		this.task = task;
	}

	public DataGrid getDg() {
		return dg;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String getOp() {
		return op;
	}

	public haipumi_task getTask() {
		return task;
	}

	public void setDg(DataGrid dg) {
		this.dg = dg;
	}
}
